﻿#region GPLv2 license
/*
Copyright (C) 2009-2010 Jamm Daemon project <http://code.google.com/p/jammd/>

Jamm is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#endregion

using System;
using System.IO;
using System.Reflection;

namespace Jamm.SharedLib.IO
{
    public static class JPath
    {
        public static string AssemblyCompany
        {
            get
            {
                var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        public static string AssemblyProduct
        {
            get
            {
                var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);

                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public static string LocalDataFolder
        {
            get
            {
                try
                {
                    // First get the 'local application data' folder
                    string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                    // Check if our company directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyCompany);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // Check if our application directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyProduct);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // check if our data folder exists, if not, create it
                    appData = Path.Combine(appData, "Data");

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    return appData;
                }
                catch
                {
                    // As a fallback, get the global temp path
                    return Path.GetTempPath();
                }
            }
        }

        public static string MyDocumentsFolder()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }

        public static string GetMyDocumentsFolder(string path)
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), path);
        }

        public static string CommonDataFolder
        {
            get
            {
                try
                {
                    // First get the 'local application data' folder
                    string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

                    // Check if our company directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyCompany);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // Check if our application directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyProduct);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // check if our data folder exists, if not, create it
                    appData = Path.Combine(appData, "Data");

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    return appData;
                }
                catch
                {
                    // As a fallback, get the global temp path
                    return Path.GetTempPath();
                }
            }
        }

        public static string LocalTempFolder
        {
            get
            {
                try
                {
                    // First get the 'local application data' folder
                    string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                    // Check if our company directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyCompany);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }


                    // Check if our application directory exists, if not, create it
                    appData = Path.Combine(appData, AssemblyProduct);

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    // check if our temp folder exists, if not, create it
                    appData = Path.Combine(appData, "Temp");

                    if (!Directory.Exists(appData))
                    {
                        Directory.CreateDirectory(appData);
                    }

                    return appData;
                }
                catch
                {
                    // As a fallback, get the global temp path
                    return Path.GetTempPath();
                }
            }
        }


        public static string GetLocalDataFolder(string relativePath)
        {
            var strPath = LocalDataFolder;

            strPath = Path.Combine(strPath, relativePath);
            return strPath;
        }

        public static string ExecutingPath
        {
            get
            {
                var strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                return strPath;
            }
        }

        public static string GetExecutingPath(string relativePath)
        {
            var strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            strPath = Path.Combine(strPath, relativePath);
            return strPath;
        }
    }
}
